package com.situ.manager.controller;

import com.situ.manager.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class UploadController {

    //MultipartFile 封装了图片上传的所有信息
    @RequestMapping("/upload")
    @ResponseBody
    public Result upload(MultipartFile file) {
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //9f1733a1136b40a8a5dc6f3ca1609be8
        String filename = file.getOriginalFilename();
        //1.jpg
        String extension = filename.substring(filename.lastIndexOf("."));
        //.jpg
        String newFilename = uuid + extension;
        //9f1733a1136b40a8a5dc6f3ca1609be8.jpg
        String filePath = "D:\\mypic\\" + newFilename;
        try {
            file.transferTo(new File(filePath));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        return Result.ok("上传成功", newFilename);
    }

    public static void main(String[] args) {
        String uuid = UUID.randomUUID().toString();
        System.out.println(uuid);
        System.out.println(uuid.replace("-", ""));
    }
}
